package com.jkc.xh.fragments;


import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jkc.xh.R;
import com.jkc.xh.adapters.HaltSellListAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.models.HaltSellModel;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.HaltSellListResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 委托撤单
 */
@EFragment(R.layout.fragment_halt_sell_list)
public class HaltSellListFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnViewClickListener {
    @ViewById(R.id.activityTitleName)
    NavBar activityTitleName;

    private HaltSellListAdapter mAdapter;
    private int pageIndex = 0;
    private int pageTotal = 0;

    @Override
    protected void initView(PullRecyclerView recyclerView) {
        mAdapter = new HaltSellListAdapter();
        mAdapter.setListener(this);
        mAdapter.setItemClickListener(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);

        activityTitleName.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                requestData(true);
            }
        });
    }


    @Override
    public void onItemClick(View view, Object object, int position) {
        final HaltSellModel model = (HaltSellModel) object;
        if (!model.isIllegleNum()) {
            ToastUtil.showToastShort("认购数量不得低于100股,不得超过最大可买数量!");
            return;
        }
        StringBuilder hintInfo= new StringBuilder();
        hintInfo.append("股票代码 : ").append(model.stock_code).append("\n");
        hintInfo.append("证券名称 : ").append(model.stock_name).append("\n");
        hintInfo.append("买入数量 : ").append(model.nowInputCount).append("\n");
        hintInfo.append("缴保证金 : ").append(model.getBalMoney(model.nowInputCount)).append("\n");
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()).setTitle("确定要认购吗?").setMessage(hintInfo.toString());
        dialogBuilder.setCancelable(true).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (view.getId()) {
                    case R.id.btnBuy:
                        addJob(TraderManager.doHaltStockBuy(model.haltsell_id, model.nowInputCount, new RestCallBack<Object>() {

                            @Override
                            public void success(Object object) {
                                requestData(true);
                                ToastUtil.showToastShort("成功");
                            }

                            @Override
                            public void failure(RestError error) {
                                ToastUtil.showToastShort(error.msg);
                            }
                        }));
                        break;
                }
            }
        }).create().show();


    }


    private void requestData(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
            mAdapter.clear();
            getPullRecyclerView().autoRefresh();
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(TraderManager.getHaltSellList(pageIndex, new RestCallBack<HaltSellListResponse>() {
            @Override
            public void success(HaltSellListResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
                    finishLoad(false);
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < Constant.PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }
            }

            @Override
            public void failure(RestError error) {
                ToastUtil.showToastShort(error.msg);
            }
        }));
    }


    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }

    @AfterViews
    void initData() {
        requestData(true);
    }
}
